<template>
<Cascader v-bind="$attrs" v-on="$listeners" :data="data" :placeholder="placeholder" v-model="house" filterable></Cascader>
</template>

<script>
export default {
    props: {
        //只接收行政区域id
        value: {
            type: [String, Number, Array],
            default: ""
        },
        buildingid: {
            type: [String, Number],
            default: ""
        },
        //返回类型（path，id或者对象）
        returnAttr: {
            type: String,
            default: "id"
        }
    },
    data() {
        return {
            data: [],
            placeholder: "请选择楼宇",
            selectItem: {},
            building: {},
            houses: [],
            house: []
        }
    },
    watch: {
        buildingid(val) {
            if (val) {
                this.initData()
            } else {
                this.house = []
                this.data = []
                this.placeholder = "请选择楼宇"
            }
        }
    },
    methods: {
        async initData() {
            this.placeholder = "数据加载中..."

            //获取楼宇信息
            let buildingRes = await this.$api.geography.buildingList({
                page: 1,
                rows: 1000,
                id: this.buildingid
            })
            this.building = buildingRes.rows[0]

            //获取房屋信息
            let houseRes = await this.$api.geography.houseList({
                page: 1,
                rows: 1000,
                buildingid: this.buildingid
            })
            this.houses = houseRes.rows

            //获取单元数
            let unitnum = parseInt(this.building.unitnum)
            //获取楼层数
            let floornum = parseInt(this.building.storeyabove)

            this.create(unitnum, floornum)

            this.placeholder = "请选择"

            if (this.value != "") {
                let houseRes = await this.$api.geography.houseList({
                    page: 1,
                    rows: 1000,
                    id: this.value
                })
                let res = houseRes.rows[0]
                this.house.push(Number.parseInt(res.unit));
                this.house.push(Number.parseInt(res.floor));
                this.house.push(Number.parseInt(res.id));
            }

        },
        create(unitnum, floornum) {
            let units = []
            for (let i = 1; i <= unitnum; i++) {
  
                let floor = []
                for (let i = 1; i <= floornum; i++) {
                    floor.push({
                        value: "floor" + i,
                        label: i + '层',
                        children: []
                    })
                }

                units.push({
                    value: "unit" + i,
                    label: i + '单元',
                    children: floor
                })
            }

            this.houses.map(item => {
                let floor = units[item.unit - 1].children[item.floor - 1]
                floor.children.push(Object.assign(item, {
                    value: "" + parseInt(item.id),
                    label: item.housenum + "室"
                }))
            })
            
            this.data = this.processData(units)
        },
        processData(units) {
            units.forEach((unit, unitIndex) => {
                unit.children.forEach((floor, floorIndex) => {
                    if (floor.children.length == 0) {
                        // unit.children.splice(floorIndex, 1)
                        floor.children.push({
                            value: 0,
                            label: "没有房屋",
                            disabled: true
                        })
                    }
                })
            })

            return units
        },
    },
    created() {
        if (this.buildingid) {
            this.initData()
        }
    }
}
</script>

<style lang="less" scoped>

</style>
